package com.stork.sms.service.all;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.stork.sms.service.attendence.StudentAttendence;
import com.stork.sms.service.attendence.date.Pattandance;
import com.stork.sms.service.fees.Fees;
import com.stork.sms.service.login.LoginDetails;
import com.stork.sms.service.login.MyPassword;
import com.stork.sms.service.login.UpdatePasswordCode;
import com.stork.sms.service.profile.StudentProfile;

public class ServiceMethods {
	
	private static final String SOAP_ACTION1 = "http://tempuri.org/loginstudentid";
    private static final String METHOD_NAME1 = "loginstudentid";
    
	private static final String SOAP_ACTION2 = "http://tempuri.org/generatepassword";
    private static final String METHOD_NAME2 = "generatepassword";
    
    private static final String SOAP_ACTION3 = "http://tempuri.org/updatepassword";
	private static final String METHOD_NAME3 = "updatepassword";
	
	private static final String SOAP_ACTION4 = "http://tempuri.org/StudentDEtails";
	private static final String METHOD_NAME4 = "StudentDEtails";
	
	private static final String SOAP_ACTION5 = "http://tempuri.org/getstudentattendance";
	private static final String METHOD_NAME5 = "getstudentattendance";
	
	private static final String SOAP_ACTION6 = "http://tempuri.org/getfees";
	private static final String METHOD_NAME6 = "getfees";
	
	private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2:2341/smsservice/webmethods.asmx";
    public static final String LOGIN_ID="username";
    
    
    StudentProfile studentProfile;
    StudentAttendence studentattendence;
    Pattandance pat;
    Fees fees;
    SoapObject request1 = new SoapObject(NAMESPACE, METHOD_NAME1);
    SoapObject request2 = new SoapObject(NAMESPACE, METHOD_NAME2);
    SoapObject request3 = new SoapObject(NAMESPACE, METHOD_NAME3);
    
    PropertyInfo properties=new PropertyInfo();
    PropertyInfo properties1=new PropertyInfo();
    
    HttpTransportSE httpTransport = new HttpTransportSE(URL,1000*60);
    
    
    String response1=null;
    String response2=null;
    String response3=null;
    String code=null;
    
    GsonBuilder gsonbuilder=new GsonBuilder();
    Gson gson=null;
    
    String getresponse=null;
    String getstudentid=null;
    String getpassword=null;
    String getupdatepassword=null;
    String notconnect="000";
    String stuattendance=null;
    
    public String Studendloginid(String studentid,String password)
    {
    	properties.name="studentid";
       	properties.type=PropertyInfo.STRING_CLASS;
       	request1.addProperty(properties,studentid);
       	
       
       	properties1.name="password";
       	properties1.type=PropertyInfo.STRING_CLASS;
       	request1.addProperty(properties1,password);
       try
       {
    	   
       	SoapSerializationEnvelope envelope1 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope1.dotNet = true;

        envelope1.setOutputSoapObject(request1);
        httpTransport.call(SOAP_ACTION1, envelope1);
        SoapPrimitive result1=(SoapPrimitive) envelope1.getResponse();
        Log.i("result111111111111", result1.toString());
        response1=result1.toString();
        
        gson=gsonbuilder.create();
        LoginDetails  logindetails=gson.fromJson(response1, LoginDetails.class);
        //Log.w("LoginDetails",logindetails.getObjcetcode());
        System.out.println("Valuee"+logindetails.getObjcetcode());
        getstudentid =logindetails.getObjcetcode();
       }
       catch(SocketException se)
       {
    	  
    	   System.out.println("socketttttt"+se.toString());
    	  
    	   return notconnect;
       }
       catch(XmlPullParserException xml)
       {
    	   System.out.println("xmllllllllll"+xml.toString());
    	   return notconnect;
       }   
       catch (IOException e) {
		// TODO Auto-generated catch block
    	   System.out.println("Inputttttttttt"+e.toString());
    	    return notconnect;
	}
        return getstudentid;
    }
    public String Getpassword(String username)
    {
    	properties.name="username";
       	properties.type=PropertyInfo.STRING_CLASS;
       	request2.addProperty(properties,username);
       	
       
       	
       try
       {
    	   
       	SoapSerializationEnvelope envelope2 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope2.dotNet = true;

        envelope2.setOutputSoapObject(request2);
        httpTransport.call(SOAP_ACTION2, envelope2);
        SoapPrimitive result2=(SoapPrimitive) envelope2.getResponse();
        Log.i("result111111111111", result2.toString());
        response2=result2.toString();
        
        gson=gsonbuilder.create();
        
        MyPassword logindetails=gson.fromJson(response2, MyPassword.class);
       // mypasswordcode passwordcode=gson.fromJson(response2, mypasswordcode.class);
        Log.w("class call", "callllllllllllllllllllllllllllll");
        //Log.w("LoginDetails",passwordcode.toString());
        //Log.w("LoginDetails111111111111",logindetails.getForgetpassword().toString());
        
        getpassword =logindetails.getForgetpassword().get(0).getPassword();
        System.out.println("valueeeeeeeee"+getpassword);
        code=logindetails.getForgetpassword().get(0).getCode();
        System.out.println("codeeeeeeeeee"+code);
        
       
       
       }
       catch(SocketException se)
       {
    	   se.printStackTrace();
    	   return notconnect;
       }
       catch(XmlPullParserException xml)
       {
    	   xml.printStackTrace();
    	   return notconnect;
       } catch (IOException e) {
		// TODO Auto-generated catch block
    	   e.printStackTrace();
    	   return notconnect;
	}
       
       if(getpassword!=null)
       {
    	
				
			
       return getpassword;
       }
       else if(code.equals("444"))
       {
    	  
				
				
       	return code;
       }
       else if(code.equals("404"))
       {
    	  
				
				
       	return code;
       }
       String data=code.concat(getpassword);
       System.out.println("dataaaaaaaaa"+data);
       return data;
    }
    public String UpdatePassword(String username,String password)
    {
    	properties.name="username";
       	properties.type=PropertyInfo.STRING_CLASS;
       	request3.addProperty(properties,username);
       	
       	properties1.name="password";
       	properties.type=PropertyInfo.STRING_CLASS;
       	request3.addProperty(properties1,password);
       
       	
       try
       {
    	   
       	SoapSerializationEnvelope envelope3 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope3.dotNet = true;

        envelope3.setOutputSoapObject(request3);
        httpTransport.call(SOAP_ACTION3, envelope3);
        SoapPrimitive result3=(SoapPrimitive) envelope3.getResponse();
        Log.i("result333333333333", result3.toString());
        response3=result3.toString();
        
        gson=gsonbuilder.create();
        
        UpdatePasswordCode logindetails=gson.fromJson(response3, UpdatePasswordCode.class);
       // mypasswordcode passwordcode=gson.fromJson(response2, mypasswordcode.class);
        Log.w("class call", "callllllllllllllllllllllllllllll");
        //Log.w("LoginDetails",passwordcode.toString());
        //Log.w("LoginDetails111111111111",logindetails.getForgetpassword().toString());
        
        getupdatepassword =logindetails.getCodepassword();
        System.out.println("updatepassworddddddd"+getupdatepassword);
        
        
       
       
       }
       catch(SocketException se)
       {
    	   se.printStackTrace();
    	   return notconnect;
       }
       catch(XmlPullParserException xml)
       {
    	   xml.printStackTrace();
    	   return notconnect;
       } catch (IOException e) {
		// TODO Auto-generated catch block
    	   e.printStackTrace();
    	   return notconnect;
	}
       
       if(getupdatepassword.equals("111"))
       {
    	
				
			
       return getupdatepassword;
       }
       else if(getupdatepassword.equals("444"))
       {
    	  
				
				
       	return getupdatepassword;
       }
       else if(getupdatepassword.equals("404"))
       {
    	  
				
				
       	return getupdatepassword;
       }
       String data=code.concat(getpassword);
       System.out.println("dataaaaaaaaa"+data);
       return data;
    }
   
    
    public StudentProfile profile(String username)
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME4);

   	 PropertyInfo properties=new PropertyInfo();
    	properties.name="studentID";
    	properties.type=PropertyInfo.STRING_CLASS;
    	String v=username;
    	request.addProperty(properties,v);
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(URL);
        
        httpTransport.debug=true;
        
        try
        {	Log.d("webservice", "inside the try");
        httpTransport.call(SOAP_ACTION4, envelope);
        SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
        Log.i("result", result.toString());
            
            System.out.println("resulttttttttttttttt"+result);
           String bracket =result.toString();
            GsonBuilder gsonBuilder=new GsonBuilder();
            Gson gson=gsonBuilder.create();
           
			 studentProfile=gson.fromJson(bracket, StudentProfile.class);
//			if(studentProfile.getCode().equals(con.mSERVICEINVALID)){
//				codeval="444";
//				invalid(codeval);
//			}
        }
        catch (SocketException exception)
        {
            System.out.println(exception);
        } catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
        return studentProfile;
    }
	public StudentAttendence Attendance(String username, String fdate, String tdate) {
		// TODO Auto-generated method stub
		 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME5);

		 PropertyInfo properties=new PropertyInfo();
		PropertyInfo properties1=new PropertyInfo();
		PropertyInfo properties2=new PropertyInfo();
		properties.name="studentid";
		properties1.name="fromdate";
		properties2.name="todate";
		properties.type=PropertyInfo.STRING_CLASS;

		request.addProperty(properties,username);
		request.addProperty(properties1,fdate);
		request.addProperty(properties2,tdate);
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
	            SoapEnvelope.VER11);
	    envelope.dotNet = true;

	    envelope.setOutputSoapObject(request);

	    HttpTransportSE httpTransport = new HttpTransportSE(URL);
	    
	    httpTransport.debug=true;
	    
	    try
	    {	Log.d("webservice", "inside the try");
	    httpTransport.call(SOAP_ACTION5, envelope);
	    SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
	    Log.i("result", result.toString());
	        
	        System.out.println("resultAAAAAAAAAAAAAAAAAAAA"+result);
	       String bracket =result.toString();
	        GsonBuilder gsonBuilder=new GsonBuilder();
	        Gson gson=gsonBuilder.create();
	       
			 studentattendence=gson.fromJson(bracket, StudentAttendence.class);
//	       
	        
	    }
	    catch (SocketException exception)
	    {
	        System.out.println(exception);
	    } catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		return studentattendence;
	}
	public Pattandance PatAttendance(String username, String fdate, String tdate) {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME5);
        
   	 PropertyInfo properties=new PropertyInfo();
   	 PropertyInfo properties1=new PropertyInfo();
   	 PropertyInfo properties2=new PropertyInfo();
    	properties.name="studentid";
    	properties1.name="fromdate";
    	properties2.name="todate";
    	properties.type=PropertyInfo.STRING_CLASS;
    	
    	request.addProperty(properties,username);
    	request.addProperty(properties1,fdate);
    	request.addProperty(properties2,tdate);
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(URL);
        
        httpTransport.debug=true;
        
        try
        {
        	httpTransport.call(SOAP_ACTION5, envelope);
            SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
            Log.i("result", result.toString());
            
            System.out.println("result PppppAttttttt"+result);
           String bracket =result.toString();
            GsonBuilder gsonBuilder=new GsonBuilder();
            Gson gson=gsonBuilder.create();
          pat=gson.fromJson(bracket, Pattandance.class);

        }
        catch (SocketException exception)
        {
            System.out.println(exception);
        } catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		return pat;

		
	}
	public Fees getfeesdetails(String username) {
		// TODO Auto-generated method stub
		 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME6);
	       
	        
	   	 PropertyInfo properties=new PropertyInfo();
	   		properties.name="studentid";
	    	properties.type=PropertyInfo.STRING_CLASS;
	    	
	    	request.addProperty(properties,username);
	       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
	                SoapEnvelope.VER11);
	        envelope.dotNet = true;

	        envelope.setOutputSoapObject(request);

	        HttpTransportSE httpTransport = new HttpTransportSE(URL);
	        
	        httpTransport.debug=true;
	        
	        try
	        {
	        	httpTransport.call(SOAP_ACTION6, envelope);
	            SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
	            Log.i("result", result.toString());
	            
	            System.out.println("fees service "+result);
	           String bracket =result.toString();
	            GsonBuilder gsonBuilder=new GsonBuilder();
	            Gson gson=gsonBuilder.create();
	            fees=gson.fromJson(bracket, Fees.class);
	          
//	          System.out.println("fdate vaaaaaaaaaaaaaaaa"+f.getFeesAmount().getTermfees().get(0).getPaymentname());
//	          System.out.println("fdate vaaaaaaaaaaaaaaaa"+baseresponse.getAttendance().getHalfdaylist().get(0).getDaylist());
//	          System.out.println("fdate vaaaaaaaaaaaaaaaa"+baseresponse.getAttendance().getHalfdaylist().get(1).getDaylist());
	          
	          
//	          todoItems.add(pat.getAttendance().getHalfdaylist().get(0).getDaylist()); 
//	          todoItems.add(pat.getAttendance().getHalfdaylist().get(1).getDaylist()); 
	        
	          }
	        catch (SocketException exception)
	        {
	            System.out.println(exception);
	        } catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}
		return fees;
	}

	
	
}
